package com.zhangx.study.demo.forkjoin.pool;

import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Random;

/**
 * @ClassName FkDao
 * @Description 演示dao操作
 * @Author zhangx
 * @Date 2021/6/3 8:47
 **/
public class FkDao {

    public static int THRESHOLD = 5000;

    private int[] makeArray(int length){
        int[] arr = new int[length];
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            arr[i] = random.nextInt(100000);
        }
        return arr;
    }

    public int getMin(){
//        select min(id) from order;
        Random r = new Random();
//        假设最小id在10~20
        return r.nextInt(10)+10;
    }

    public int getMax(){
//        select max(id) from order;
        Random r = new Random();
//        假设最大id在千万
        return r.nextInt(10)+1000*10000;//1000*万
    }

    /**
     * 模拟进行分批，每批数据THRESHOLD
     * @param fromId
     * @param toId
     * @return
     */
    public long sum(int fromId,int toId){
        long sum = 0;
//        select money from order where id>=fromId and id<=toId
        try {
//            模拟数据库查询
            Thread.sleep(15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int[] arr = this.makeArray(THRESHOLD);
        for (int t:arr) {
            sum += t;
        }
        return sum;
    }
}
